import { NResult, type ResultProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type ResultFactoryConstructorType = {
  props?: ResultProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
};

export class ResultFactory {
  private attrs: HTMLAttributes = {};
  private props: ResultProps = {};
  private defaultSlot: BaseComponentType = null;
  private footerSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  constructor(data?: ResultFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.footerSlot) this.setFooter(data.footerSlot);
    if (data?.iconSlot) this.setIcon(data.iconSlot);
  }
  setProps(props: ResultProps) {
    this.props = props;
    return this;
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }
  setFooter(footerSlot: BaseComponentType) {
    this.footerSlot = footerSlot;
    return this;
  }
  setIcon(iconSlot: BaseComponentType) {
    this.iconSlot = iconSlot;
    return this;
  }
  create() {
    return (
      <NResult {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          footer:
            this.footerSlot != null ? () => getSlotsDom(this.footerSlot) : null,
          icon: this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
        }}
      </NResult>
    );
  }
}
